//
//  CLReadMarkCell.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/18.
//

#import "CLReadMarkCell.h"
#import "NSDate+CLUtility.h"
@implementation CLReadMarkCell

+ (CLReadMarkCell *)cell:(UITableView *)tableView  {
    
    CLReadMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CLReadMarkCell"];
    
    if (cell == nil) {
        cell = [[CLReadMarkCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CLReadMarkCell"];
    }

    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = UIColor.clearColor;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.title = [[UILabel alloc]init];
    self.title.font = CL_FONT_SA_14;
    self.title.textColor = CL_COLOR_145_145_145;
    [self.contentView addSubview:self.title];

    self.time = [[UILabel alloc]init];
    self.time.font = CL_FONT_SA_12;
    self.time.textColor = CL_COLOR_145_145_145;
    self.time.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.time];

    self.content = [[UILabel alloc]init];
    self.content.font = CL_FONT_SA_14;
    self.content.textColor = CL_COLOR_145_145_145;
    self.content.numberOfLines = 0;
    [self.contentView addSubview:self.content];

    self.spaceLine = SpaceLine(self.contentView, CL_COLOR_230_230_230);
    
}

- (void)setMarkModel:(CLReadMarkModel *)markModel {
    _markModel = markModel;
    self.title.text = markModel.name;
    self.time.text = [NSDate timerString:markModel.time];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:markModel.content];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = CL_SPACE_5;// 字体的行间距
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    [attributedText setAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, markModel.content.length)];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    
    CGFloat itemX = CL_SIZE(CL_SPACE_15);
    CGFloat itemY = CL_SIZE(CL_SPACE_15);
    CGFloat itemH = CL_SIZE(CL_SPACE_20);
    CGFloat contentW = w - 2*itemX;
    CGFloat contentY = itemY + itemH + CL_SIZE(CL_SPACE_10);
    CGFloat itemW = (contentW - CL_SIZE(CL_SPACE_10)) / 2;
    
    
    self.title.frame = CGRectMake(itemX, itemY, itemW, itemH);
    
    self.time.frame = CGRectMake(w - itemW - itemX, itemY, itemW, itemH);
    
    self.content.frame = CGRectMake(itemX, contentY, contentW, CL_SIZE(CL_SPACE_100) - contentY - CL_SIZE(CL_SPACE_15));
    
    self.spaceLine.frame = CGRectMake(CL_SIZE(CL_SPACE_15), h - CL_SPACE_LINE, w - CL_SIZE(CL_SPACE_30), CL_SPACE_LINE);
}
@end
